/**
 * Created by 白雨浓 on 2018/1/24 11:22.
 */

import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';

export default {
    xml(self, name, entityPackage, table){
        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, 'mapperXML.ejs');
        if (!fs.existsSync(filePath)) {
            self.$$msg.error('mapperXML模板文件缺失');
            return;
        }
        const tmpFile = fs.readFileSync(filePath, 'utf-8');

        let clsName = self.$str.toCamel(name);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        const mappers = [];

        const insertMethod = this.getMethod(table.methods, 'insert');
        if (insertMethod) {
            mappers.push(`<insert id="insert">
                ${table.insert}
        </insert>`);
        }

        const updateMethod = this.getMethod(table.methods, 'update');
        if (updateMethod) {
            mappers.push(`<update id="update">
                ${table.update}
        </update>`);
        }

        const selectMethod = this.getMethod(table.methods, 'select');
        if (selectMethod) {
            mappers.push(`<select id="select" resultType="${entityPackage}">
                ${table.select}
        </select>`);
        }

        const selectListMethod = this.getMethod(table.methods, 'selectList');
        if (selectListMethod) {
            mappers.push(`<select id="selectList" resultType="${entityPackage}">
                ${table.selectList}
        </select>`);
        }

        const selectPageMethod = this.getMethod(table.methods, 'selectPage');
        if (selectPageMethod) {
            mappers.push(`<select id="selectPage" resultType="${entityPackage}">
                ${table.selectPage}
        </select>`);
        }

        const selectPageTotalMethod = this.getMethod(table.methods, 'selectPageTotal');
        if (selectPageTotalMethod) {
            mappers.push(`<select id="selectPageTotal" resultType="java.lang.Integer">
                ${table.selectPageTotal}
        </select>`);
        }

        const selectByKeywordMethod = this.getMethod(table.methods, 'selectByKeyword');
        if (selectByKeywordMethod) {
            mappers.push(`<select id="selectByKeyword" resultType="${entityPackage}">
                ${table.selectByKeyword}
        </select>`);
        }

        const selectTotalByKeywordMethod = this.getMethod(table.methods, 'selectTotalByKeyword');
        if (selectTotalByKeywordMethod) {
            mappers.push(`<select id="selectTotalByKeyword" resultType="java.lang.Integer">
                ${table.selectTotalByKeyword}
        </select>`);
        }

        const tmp = {
            packageName: self.config.packageName,
            clsName: clsName,
            mappers: mappers
        };

        return ejs.render(tmpFile, tmp);
    },
    getMethod(methods, name){
        for (const method of methods) {
            if (method.name === name)
                return method;
        }
    }
}